package com.wolfking.converter.rowmapper;

import com.wolfking.converter.util.FieldConverterUtil;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

@SuppressWarnings("all")
public abstract class AbstractFieldRowMapper<T> extends AbstractCamelRowMapper<T> {

    private Class<T> aClass;

    public AbstractFieldRowMapper() {
        Type type = getClass().getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            ParameterizedType pType = (ParameterizedType) type;
            Type claz = pType.getActualTypeArguments()[0];
            if (claz instanceof Class) {
                this.aClass = (Class<T>) claz;
            }
        } else {
            throw new RuntimeException("not T parameter");
        }
    }

    @Override
    protected T convertMapToObject(Map<String, Object> map) {
        return FieldConverterUtil.assemblyObjectIgnoreException(map, this.aClass);
    }
}
